<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 下面解构 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit? '编辑':'添加'"
      :close-on-click-modal="false"
      :close-on-press-escap="false"
      :visible.sync="showDialog"
      @close="hDialogClose"
    >
      <!-- v-if="showDialog" -->
      <addor-edit
        :id="curId"
        ref="refDeptDialog"
        :is-edit="isEdit"
        :origin-list="originList"
        @update-depart="hUpdateDepart"
        @close="hCancel"
      />
    </el-dialog>

  </div>

</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog'
export default {
  components: {
    'addor-edit': AddorEdit
  },
  data() {
    return {
      isEdit: false, // 当前是否处于编辑状态
      curId: '', // 当前操作的部门Id
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      originList: []// 简洁数据,表单校验

    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        const res = await getDepartments()
        // console.log(res)
        // 取出第一个元素
        const root = res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id, pid: item.pid, name: item.name, code: item.code
          }
        })
        const children = tranListToTreeData(res.data.depts)
        root.children = children
        this.list = [root]
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(id) {
      // console.log(123, id)
      this.curId = id
      this.showDialog = true
      this.isEdit = false // 更新编辑状态
    },
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 更新数组
        this.loadDepartment()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败!' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 1. 去做删除
        this.doDel(id)
        // // 3. 更新页面- 重发请求
        // this.loadDepartments()
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    hEdit(id) {
      // 1. 更新当前操作的部门id
      this.curId = id
      this.isEdit = true
      // console.log(id)
      this.showDialog = true
      //  强制让子组件去发请求获取详情
      console.log('强制让子组件去发请求获取详情', this.$refs.refDeptDialog)
      this.$nextTick(() => {
        this.$refs.refDeptDialog.loadDetail()
      })
    },
    hUpdateDepart() {
      this.showDialog = false
      this.loadDepartment()
    },
    hCancel() {
      this.showDialog = false
    },
    hDialogClose() {
      // console.log(123)
      this.$refs.refDeptDialog.resetForm()
    }
  }
}
</script>
